package cn.wmlce.web.ch4_5;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ValueConstants;

import java.util.Random;

/**
 * SSE(Server Send Event) 服务端发送事件
 */
@Controller
public class SseController {

    /**
     * 这里使用输出的媒体类型为"text/event-stream",这是服务器端SSE的支持。本例演示每5秒钟向浏览器推送随机信息
     * @return
     */
    @RequestMapping(value = "/push",produces = "text/event-stream")
    public @ResponseBody String push(){
        Random r = new Random();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "data:Testing 1,2,3" + r.nextInt()+"\n\n";
    }
}
